package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.R;
import com.example.entity.User;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户信息(User)表控制层
 *
 * @author makejava
 * @since 2022-05-26 17:39:32
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
   @PostMapping("/login")
    public R<String>login(@RequestBody User user, HttpServletRequest request){
       LambdaQueryWrapper<User>queryWrapper=new LambdaQueryWrapper<>();
       queryWrapper.eq(User::getPhone,user.getPhone());
       User user2=userService.getOne(queryWrapper);
       if (user2!=null){
           request.getSession().setAttribute("user",user2.getId());
           log.info("用户登录名"+request.getSession().getAttribute("user"));
           return R.success("登陆成功");
       }else{
           return R.error("登陆失败该账号不存在");
       }

   }

}